#include "BlackBear.h"
#include "TranslateNode.h"
#include "RotationNode.h"
#include "GeomBuilder.h"

void BlackBear::update(void) {
	float x = loc.getVal(0);
	if (turning) {
		float currentYaw = ((RotationNode*)rotWholeBear)->getYaw();
		if (dir == 0) {
			((RotationNode*)rotWholeBear)->setYPR(currentYaw + D3DX_PI/20,0,0);
			if (((RotationNode*)rotWholeBear)->getYaw() >= D3DX_PI/1.2)
				x += length/17;
			((TranslateNode*)transNode)->setX(x);

			if (((RotationNode*)rotWholeBear)->getYaw() >= D3DX_PI) {
				turning = false;
				((RotationNode*)rotWholeBear)->setYPR(D3DX_PI,0,0);
			}
		}
		else if (dir == 1) {
			((RotationNode*)rotWholeBear)->setYPR(currentYaw + D3DX_PI/20,0,0);
			if (((RotationNode*)rotWholeBear)->getYaw() <= D3DX_PI*1.8)
				x -= length/17;
			((TranslateNode*)transNode)->setX(x);

			if (((RotationNode*)rotWholeBear)->getYaw() >= 2 * D3DX_PI) {
				turning = false;
				((RotationNode*)rotWholeBear)->setYPR(0,0,0);
			}
		}

		loc.setX(x);
	}
	else {
		float patrolBounds = 15;
		if (dir == 1)
			x+=0.25;
		else
			x-=0.25;

		((TranslateNode*)transNode)->setX(x);
		if (x >= startLoc.getVal(0) + patrolBounds && dir == 1) {
			dir = 0;
			turning = true;
			//((RotationNode*)rotWholeBear)->setYPR(D3DX_PI,0,0);
			//x += length;
			//((TranslateNode*)transNode)->setX(x);
		}
		else if (x <= startLoc.getVal(0) - patrolBounds && dir == 0) {
			dir = 1;
			turning = true;
			//((RotationNode*)rotWholeBear)->setYPR(0,0,0);
			//x -= length;
			//((TranslateNode*)transNode)->setX(x);
		}
		
		loc.setX(x);
	}
}

BlackBear::BlackBear(float r, Location l) : dir(1), turning(false) {
	Bear::size = 100000;
	length = r;
	Bear::loc = *(new Location(l));
	Bear::drawMe = true;
	startLoc = *(new Location(l));

	//main translate for whole bear
	transNode = new TranslateNode(loc);
	rotWholeBear = new RotationNode(0,0,0);
	transNode->AddChild(rotWholeBear);
	
	//bear body
	//SceneNode * rotNode = new RotationNode(&D3DXVECTOR3(1.0f,0,0),D3DX_PI/1.5);
	SceneNode * rotNode = new RotationNode(0,0,-D3DX_PI/2);
	GeomNode * bearBody = BuildCylinder(r/4, r, 16);
	bearBody->SetMaterial(0.2f,0.2f,0.2f);
	rotNode->AddChild(bearBody);
	rotWholeBear->AddChild(rotNode);

	//bear legs
	SceneNode * transLeg1 = new TranslateNode(2.0f,float(-r/1.75),-1.0f);
	GeomNode * bearLeg = BuildCylinder(r/10, r/2, 8);
	bearLeg->SetMaterial(0.2f,0.2f,0.2f);
	transLeg1->AddChild(bearLeg);
	rotWholeBear->AddChild(transLeg1);

	SceneNode * transLeg2 = new TranslateNode(2.0f,float(-r/1.75),1.0f);
	transLeg2->AddChild(bearLeg);
	rotWholeBear->AddChild(transLeg2);

	SceneNode * transLeg3 = new TranslateNode(float(r-2),float(-r/1.75),-1.0f);
	transLeg3->AddChild(bearLeg);
	rotWholeBear->AddChild(transLeg3);

	SceneNode * transLeg4 = new TranslateNode(float(r-2),float(-r/1.75),1.0f);
	transLeg4->AddChild(bearLeg);
	rotWholeBear->AddChild(transLeg4);

	//bear head
	SceneNode * transHead = new TranslateNode(float(r+1),float(r/4.5),0.0f);
	//SceneNode * rotHead = new RotationNode(&D3DXVECTOR3(1.0f,1.0f,0),D3DX_PI/2);
	GeomNode * bearHead = BuildSphere(float(r/4.5), 8, 8);
	bearHead->SetMaterial(0.2f,0.2f,0.2f);
	transHead->AddChild(bearHead);
	//rotHead->AddChild(transHead);
	rotWholeBear->AddChild(transHead);




	myDrawable.AddChild(transNode);
}

BlackBear::~BlackBear() {
	delete(transNode);
	delete(rotWholeBear);
}

void BlackBear::setDrawable(SceneRoot &newRoot) {
	Bear::myDrawable = newRoot;
}

void BlackBear::setSize(int newSize) {
	Bear::size = newSize;
}

bool BlackBear::move(float xd, float yd) {
	//do work to move the bear
	return Bear::move(xd, yd);
}

bool BlackBear::attack(float range) {
	//attack in facing direction at range
	return Bear::attack(range);
}

bool BlackBear::deleteMe(void) {
	return deleteThis;
}

void BlackBear::setDelete(bool d) {
	deleteThis = d;
}